package com.androway.model;

import java.util.List;

import com.androway.domains.User;
import com.androway.services.Requester.OnResponseListener;
import com.androway.services.Requester.RequestType;
import com.androway.utils.MD5;

public class LoginModel extends AbstractModel
{

	public LoginModel()
	{
		OnResponseListener requestListener = new OnResponseListener()
		{
			@Override
			public void onSuccess(RequestType type, List<?> objects, String service)
			{
				ApplicationModel.setUser((User) objects.get(0));
				LoginModel.this.listener.onSuccess(type, service);
			}

			@Override
			public void onError(RequestType type, String errorMessage)
			{
				System.err.println(errorMessage);
				LoginModel.this.listener.onError(type, errorMessage);
			}
		};
		this.requester.setListener(requestListener);
	}

	public void connect(String username, String password)
	{
		this.requester.setAuthentification(username, MD5.hash(password));
		this.get(ApplicationModel.serverUrl + "Challenge", User.class, "Challenge");
	}
}
